<?php

namespace app\api\admin\v1\service\finder;

use zhanshop\App;
use extend\docker\Images;
use zhanshop\client\Httpclient;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Cloudstore extends BaseFinder
{
    protected $headToolbar = [];

    protected $search = false;
    protected $storeApi = '';

    protected $rowToolbar = [
        [
            'event' => 'ajax',
            'ico' => '&#xe642;',
            'title' => '下载',
            'method' => 'load',
            'page' => '',
        ],
    ];

    public function __construct(string $menuId)
    {
        parent::__construct($menuId);
        $this->storeApi = 'http://'.App::config()->get('bridge.ip').':'.(intval(App::config()->get('bridge.port')) - 1).'/v1/cloud.images/'.App::config()->get('bridge.token');
    }

    protected function getCols(string $schma){
        return [
            'id' => array (
                'field' => 'id',
                'width' => 110,
                'title' => 'ID',
                'search' => true,
                'input_type' => 'hidden',
            ),
            'image_name' => array (
                'field' => 'image_name',
                'title' => '名称',
                'width' => 280,
                'search' => true,
                'input_type' => 'text',
            ),
            'description' => array(
                'field' => 'description',
                'title' => '描述',
                'width' => 280,
                'input_type' => 'hidden',
            ),
            'size' => array (
                'field' => 'size',
                'title' => '大小',
                'width' => 170,
                'input_type' => 'hidden',
            ),
            'create_time' => array (
                'field' => 'create_time',
                'input_type' => 'time',
                'title' => '发布时间',
                'width' => 170
            )
        ];
    }

    public function data(int $page, int $limit, array $order, array $search)
    {
        $resp = (new Httpclient())->enableIpv6()->request($this->storeApi."?page=".$page."&limit=".$limit);
        $body = json_decode($resp['body'], true);
        $total = 0;
        $list = [];
        if($body){
            $total = $body['total'] ?? 0;
            $list = $body['list'] ?? [];
            foreach ($list as $k => $v){
                $json = json_decode($v['image_url'] ?? '', true);
                $size = $json['size'] ?? -1;
                $list[$k]['size'] = round(($size / 1024 / 1024), 2).'MB';
                $list[$k]['url'] = $json['image_url'] ?? '';
            }
        }
        return [
            'total' => $total,
            'list' => $list
        ];
    }

    public function load(Request $request, Response $response)
    {
        $input = $request->validateRule([
            'id' => 'Required',
        ])->getData();
        $list = (new Httpclient())->enableIpv6()->request($this->storeApi."?&id=".$input['id']."&page=1&limit=1");
        $list = json_decode($list['body'], true);
        if($list && isset($list['list'][0]['image_url'])){
            $arr = json_decode($list['list'][0]['image_url'], true);
            if($arr['url']){
                App::task(Images::class)->load($arr['url']);
            }
        }
        return '任务已投递,请稍后在镜像列表查看';
    }
}